package com.nintendo.frame.edit;

import com.nintendo.entity.pojo.Desk;
import com.nintendo.frame.DeskFrame;
import com.nintendo.mapper.DeskDao;
import com.nintendo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateDeskFrame extends JFrame {
    private JTextField nameField;
    private JTextField seatsField;
    private JComboBox<String> statusComboBox;
    private JButton saveButton;
    private Desk desk;
    private DeskFrame parentFrame;

    public UpdateDeskFrame(Desk desk, DeskFrame parentFrame) {
        this.desk = desk;
        this.parentFrame = parentFrame;
        setTitle("更新餐台");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建面板
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(4, 2, 10, 10));

        // 名称标签和文本框
        JLabel nameLabel = new JLabel("名称:");
        nameField = new JTextField(desk.getNo());
        inputPanel.add(nameLabel);
        inputPanel.add(nameField);

        // 座位数标签和文本框
        JLabel seatsLabel = new JLabel("座位数:");
        seatsField = new JTextField(String.valueOf(desk.getSeating()));
        inputPanel.add(seatsLabel);
        inputPanel.add(seatsField);

        // 状态标签和组合框
        JLabel statusLabel = new JLabel("状态:");
        String[] statuses = {"可用", "不可用", "维护中"};
        statusComboBox = new JComboBox<>(statuses);
        statusComboBox.setSelectedItem(desk.getStatus());
        inputPanel.add(statusLabel);
        inputPanel.add(statusComboBox);

        // 保存按钮
        saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveUpdatedDesk();
            }
        });

        // 设置内容面板
        getContentPane().add(inputPanel, BorderLayout.CENTER);
        getContentPane().add(saveButton, BorderLayout.SOUTH);
    }

    private void saveUpdatedDesk() {
        String name = nameField.getText();
        String seatsStr = seatsField.getText();
        String status = (String) statusComboBox.getSelectedItem();

        if (!name.isEmpty() && !seatsStr.isEmpty()) {
            int seats = Integer.parseInt(seatsStr);
            desk.setNo(name);
            desk.setSeating(seats);
            desk.setStatus(status);
            try (SqlSession session = MyBatisUtils.getSqlSession()) {
                DeskDao mapper = session.getMapper(DeskDao.class);
                mapper.update(desk);
                session.commit();
            }
            parentFrame.loadDataFromDatabase(); // 刷新父窗口的数据
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "请填写完整信息");
        }
    }

    public static void showUpdateDeskFrame(Desk desk, DeskFrame parentFrame) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new UpdateDeskFrame(desk, parentFrame).setVisible(true);
            }
        });
    }
}
